package com.zhongyewx.teachercert.framework.okhttp;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public class ZYSubscriber<T> implements Observer<T> {

    private ZYOnHttpCallBack<T> mCallBack;

    public ZYSubscriber(ZYOnHttpCallBack<T> callBack){
        this.mCallBack = callBack;
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
//        mCallBack.onFaild("亲，请检查网络！");
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            int code = httpException.code();
            if (code == 500 || code == 404) {
                mCallBack.onFaild("服务器出错");
            }
        } else if (e instanceof ConnectException) {
            mCallBack.onFaild("网络断开,请打开网络!");
        } else if (e instanceof SocketTimeoutException) {
            mCallBack.onFaild("网络连接超时!!");
        } else if (e instanceof UnknownHostException) {
            mCallBack.onFaild("网络异常!");
        } else {
            mCallBack.onFaild("");
//            mCallBack.onFaild("发生未知错误" + e.getMessage());
//            e.printStackTrace();
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(T t) {
        mCallBack.onSuccessful(t);
    }
}